-- 快捷键和快捷键提示
vim.keymap.set({ 'n' }, '<leader>m', '<cmd> Alpha <cr>', { desc = "toggle start page" })

local function augroup(name)
  return vim.api.nvim_create_augroup("lazyvim_" .. name, { clear = true })
end
-- Check if we need to reload the file when it changed
vim.api.nvim_create_autocmd({ "FocusGained", "TermClose", "TermLeave" }, {
  group = augroup("checktime"),
  callback = function()
    if vim.o.buftype ~= "nofile" then
      vim.cmd("checktime")
    end
  end,
})


-- go to last loc when opening a buffer
vim.api.nvim_create_autocmd("BufReadPost", {
  group = augroup("last_loc"),
  callback = function(event)
    local exclude = { "gitcommit" }
    local buf = event.buf
    if vim.tbl_contains(exclude, vim.bo[buf].filetype) or vim.b[buf].lazyvim_last_loc then
      return
    end
    vim.b[buf].lazyvim_last_loc = true
    local mark = vim.api.nvim_buf_get_mark(buf, '"')
    local lcount = vim.api.nvim_buf_line_count(buf)
    if mark[1] > 0 and mark[1] <= lcount then
      pcall(vim.api.nvim_win_set_cursor, 0, mark)
    end
  end,
})

-- Fix conceallevel for json files
vim.api.nvim_create_autocmd({ "FileType" }, {
  group = augroup("json_conceal"),
  pattern = { "json", "jsonc", "json5" },
  callback = function()
    vim.opt_local.conceallevel = 0
  end,
})



local new_cmd = vim.api.nvim_create_user_command

local config = require 'next.config'

new_cmd("LazyClean", function()
  vim.cmd('!rm -rf ' .. config.plugins.install_path)
end, {})
new_cmd("Zshrc", function()
  vim.cmd('e ~/.zshrc')
end, {})

new_cmd("ConfigNvim", function()
  vim.cmd('e ' .. vim.fn.stdpath('config')..'/lua/next/init.lua')
end, {})

new_cmd("Make", function ()
  vim.cmd('!cmake ./ && make')
end, {})

return {
  {
    'numToStr/Comment.nvim',
    lazy = false,
    config = function()
      local esc = vim.api.nvim_replace_termcodes(
        '<ESC>', true, false, true
      )

      vim.keymap.set('x', '<leader>/', function()
        vim.api.nvim_feedkeys(esc, 'nx', false)
        require('Comment.api').toggle.linewise(vim.fn.visualmode())
      end)


      vim.keymap.set('n', '<leader>/', require('Comment.api').toggle.linewise.current, {
        desc = 'toggle comment'
      })
      vim.keymap.set('x', '<leader>/', function()
        vim.api.nvim_feedkeys(esc, 'nx', false)
        require('Comment.api').toggle.linewise(vim.fn.visualmode())
      end)
    end
  },
  {
    "JoosepAlviste/nvim-ts-context-commentstring",
    event = "VeryLazy",
  },
  {
    "folke/todo-comments.nvim",
    event = "VeryLazy",
    opts = {
      -- your configuration comes here
      -- or leave it empty to use the default settings
      -- refer to the configuration section below
    },
  },
  {
    "danymat/neogen",
    opts = {}
  },
  -- {
  --   "vhyrro/luarocks.nvim",
  --   priority = 1000,
  --   config = true,
  --   opts = {
  --     rocks = { "lua-curl", "nvim-nio", "mimetypes", "xml2lua" }
  --   }
  -- },
  {
    "ggandor/leap.nvim",
    branch = "main",
    config = function()
      -- require('leap').create_default_mappings()
      vim.keymap.set({ 'n', 'x', 'o' }, 'S', "<Plug>(leap-forward)", { desc = "Leap from forward" })
      vim.keymap.set({ 'n', 'x', 'o' }, 'K', "<Plug>(leap-backward)", { desc = "Leap from backword" })
    end,
    dependencies = { "tpope/vim-repeat" }
  },
  {
    "kylechui/nvim-surround",
    version = "*", -- Use for stability; omit to use `main` branch for the latest features
    event = "VeryLazy",
    opts = {},
  },

}
